package cn.woblog.android.downloader.test;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import cn.woblog.android.downloader.R;
import cn.woblog.android.downloader.manager.DownloadManager;
import cn.woblog.android.downloader.service.DownloadService;

import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.DbException;
import com.lidroid.xutils.util.LogUtils;
import com.lidroid.xutils.view.annotation.event.OnClick;

public class MainActivity extends ActionBarActivity {

	private static final String TEST_URL = "http://bcs.duapp.com/testdownloaddata/jingdong.apk";
	private DownloadManager downloadManager;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		ViewUtils.inject(this);
		downloadManager = DownloadService.getDownloadManager(this);
	}

	@OnClick(R.id.download_btn)
	public void download(View view) {
		for (int i = 0; i < 50; i++) {
			String target = "/sdcard/xUtils/" + System.currentTimeMillis()
					+ "lzfile.apk";
			try {
				downloadManager.addNewDownload(TEST_URL, "力卓文件" + i, target // 如果目标文件存在，接着未完成的部分继续下载。服务器不支持RANGE时将从新下载。
						, null);
			} catch (DbException e) {
				LogUtils.e(e.getMessage(), e);
			}
		}

	}

	@OnClick(R.id.download_page_btn)
	public void downloadPage(View view) {
		Intent intent = new Intent(this, DownloadListActivity.class);
		startActivity(intent);
	}
}
